<?php if(count($plans)){
	$selectedPlanId = isset($this->data['ListingPlan']) ? @$this->data['ListingPlan']['plan_id'] : null;
	$plansJsObject = array();
	?>
		<h4><?php echo __("Select Plans")?></h4>
		<?php 
		
		$restrictCustomFields = false;
		foreach($plans as $plan)
		{
			if($plan['Plan']['custom_fields']['fieldsets'] || $plan['Plan']['custom_fields']['fields'])
			{
				$restrictCustomFields = true;
				break;
			}
		}
		foreach($plans as $plan){?>
			<div class="plan_info">
				<?php 
					$checked = "";
					$inputId = "PlanInfo".$plan['Plan']['id'];
					if($selectedPlanId && $selectedPlanId == $plan['Plan']['id'])
					{
						$checked = "checked=\"checked\"";
					}else if(!$selectedPlanId && $plan['Plan']['is_default'])
					{
						$checked = "checked=\"checked\"";
					}
				?>
				<input type="hidden" name="data[ListingPlan][__plan]" value="1" />
				<input type="radio" class="plan_select" name="data[ListingPlan][plan_id]" value="<?php echo $plan['Plan']['id']?>" id="<?php echo $inputId?>" <?php echo $checked?>/>
				<label for="<?php echo $inputId?>"><?php echo $plan['Plan']['title']?>
					<span class="clear"></span>
					- <span class="price_plan"><?php 
						$price = 0;
						if($plan['Plan']['price'] && $plan['Plan']['payment_type'] != Plan::PAYMENT_TRIAL)
						{
							$price = $plan['Plan']['price'];
						}
						$period = "";
						if($plan['Plan']['payment_type'] == Plan::PAYMENT_SUBSCRIPTION)
						{
							$durationOptions = cri("PaidListings.Plan")->getFieldProperty('duration_period','options');
							$periodLabel = $durationOptions[$plan['Plan']['duration_period']];
							$periodLabel = str_replace("(s)", "", $periodLabel);
							if($plan['Plan']['duration_number'] == 1)
							{
								$periodLabel = __($periodLabel);
								$period = String::insert(__(" per :duration"),array('duration'=>$periodLabel));
							}else
								{
									$periodLabel = __($periodLabel.'s');
									$period = String::insert(__(" every :duration_number :duration"),array("duration_number"=>$plan['Plan']['duration_number'],'duration'=>$periodLabel));
								}
						}
						echo cr('PaidListingConfig.currency_symbol').$price.$period?></span>
				</label>
				<div class="plan_description">
				<?php echo $plan['Plan']['description']; ?>
				</div>
			</div>
		
		<?php 
			$plansJsObject[$plan['Plan']['id']] = array('restrictCustomFields'=>$restrictCustomFields,'allowedFieldSets'=>$plan['Plan']['custom_fields']['fieldsets'],
							'allowedFields'=>$plan['Plan']['custom_fields']['fields'],'allow_multiple_categories'=>$plan['Plan']['allow_multiple_categories']);
		} ?>
		<?php if(isset($manipluateFields) && $manipluateFields){?>
		<script type="text/javascript">
			var ListingPlans = <?php echo $javascript->object($plansJsObject)?>;
			var filterPlanForm = function()
			{
				var selectedPlanId = $('.plan_select:checked').val();
				var customFields = $(".custom_field");
				var fieldsets = $('.fieldset');
				var inputSelector = "input,select,textarea";
				if(selectedPlanId && typeof ListingPlans[selectedPlanId] == "object")
				{
						fieldsets.show();
						customFields.find(inputSelector).attr('disabled',false);
						var allowedFieldsets = ListingPlans[selectedPlanId].allowedFieldSets;
						var allowedCustomFields = ListingPlans[selectedPlanId].allowedFields;
						if(typeof ListingPlans[selectedPlanId].allow_multiple_categories != "undefined" && ListingPlans[selectedPlanId].allow_multiple_categories == 1)
            {
                $("#MultipcategorySelector").trigger("enableSelector")
            }else
             {
                 $("#MultipcategorySelector").trigger("disableSelector")
             }
						fieldsets.each(function (){
							var childCustomFields = $(this).find(".custom_field");
							var fieldsetId = $(this).attr("id").split("_").pop();
							if(ListingPlans[selectedPlanId].restrictCustomFields && $.inArray(fieldsetId,allowedFieldsets) == -1)
							{
								var hiddenCustomFieldCount = 0;
								childCustomFields.each(function(){
									var domId = $(this).attr("id").split("_").pop();
									if($.inArray(domId,allowedCustomFields) == -1)
									{
										hiddenCustomFieldCount++;
										$(this).hide('slow').find(inputSelector).attr("disabled",true);
									}else
									{
										$(this).show('slow').find(inputSelector).attr("disabled",false);
									}
								});
								if(hiddenCustomFieldCount == childCustomFields.length)
								{
									$(this).hide('slow');
								}else
								{
									$(this).show('slow');
								}
							}else
							{
								$(this).show();
								childCustomFields.show().find(inputSelector).attr("disabled",false);
							}
					});
				}else
				{
					fieldsets.hide();
					customFields.find(inputSelector).attr('disabled',true);
				} 
			}
			$(document).ready(function ()
			{
				filterPlanForm();
				$('.plan_select').change(function (){
					filterPlanForm();
				});
			});
		</script>
		<?php } ?>
<?php } ?>
<div class="clear"></div>